package com.yuyou.hxyy.gateway.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.hxyy.service.api.bean.LoginRedisObj;
import com.yuyou.hxyy.service.api.bean.ResBoolSimpleInfo;
import com.yuyou.hxyy.service.api.bean.ReturnResult;
import com.yuyou.hxyy.service.api.security.SecurityCons;
import com.yuyou.hxyy.service.api.service.ILoginService;
import com.yuyou.hxyy.service.api.vo.req.ReqLogin;
import com.yuyou.hxyy.service.api.vo.req.ReqPassWordReset;
import com.yuyou.hxyy.service.api.vo.req.ReqPassWordUpdate;
import com.yuyou.hxyy.service.api.vo.resp.RespLogin;

@Api(value="LoginController",description="登录密码接口")
@Validated
@RestController
@RequestMapping(value ="/api")
public class LoginController extends BaseController{

	
	@Autowired
	private ILoginService loginService;
	
	@ApiOperation(value="登录")
	@RequestMapping(value="/login" ,method=RequestMethod.POST)
	public ReturnResult<RespLogin> login(@Validated @ModelAttribute ReqLogin reqLogin){
		ResBoolSimpleInfo<RespLogin> res=loginService.login(reqLogin);
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(), res.getData());
	}
	
	@ApiOperation(value="修改密码")
	@RequestMapping(value="/updatePass" ,method=RequestMethod.POST)
	public ReturnResult<String> updatePass(HttpServletRequest request,@Validated @ModelAttribute ReqPassWordUpdate reqPassWordUpdate){	
		LoginRedisObj user = this.getUserObj(request);
		ResBoolSimpleInfo<String> res=loginService.updatePass(user.getUserId(),reqPassWordUpdate);
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(), res.getData());		

	}
	
	@PreAuthorize(SecurityCons.admin)
	@ApiOperation(value="重置密码")
	@RequestMapping(value="/resetPass" ,method=RequestMethod.POST)
	public ReturnResult<String> resetPass(HttpServletRequest request,@Validated @ModelAttribute ReqPassWordReset reqPassWordReset){	
		LoginRedisObj user = this.getUserObj(request);
		ResBoolSimpleInfo<String> res=loginService.resetPass(user.getUserId(),reqPassWordReset);
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(), res.getData());		

	}
	
}
